package 贪心;

public class q122买卖股票最好时机 {

    public static void main(String[] args) {

        int[] prices = {1,2,3,4,5};
        Solution solution = new q122买卖股票最好时机().new Solution();
        System.out.println(solution.maxProfit(prices));
    }

    class Solution {
        public int maxProfit(int[] prices) {
            if (prices.length == 1) return 0;
            int res = 0;
            int[] profit = new int[prices.length - 1];
            for (int i = profit.length - 1; i >= 0; i--) {
                profit[i] = prices[i+1] - prices[i];
                if(profit[i] > 0){
                    res += profit[i];
                }
            }


            return res;

        }
    }
}
